package com.egene.tw.activity.main;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.base.BaseActivity;
import com.egene.tw.utils.MToast;
import com.google.android.gms.common.GoogleApiAvailability;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;


@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity implements TabHost.OnTabChangeListener, IMainTab {
    @ViewById(android.R.id.tabhost)
    FragmentTabHost mTabHost;
    @ViewById(R.id.actionBar)
    Toolbar mToolbarView;
    @ViewById(R.id.toolbar_title)
    TextView mToolbarTitle;

    @Extra
    int startIndex;

    private boolean mScrolled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        检测google服务版本是否可用 （fcm推送需要google服务）
        GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //检测google服务版本是否可用 （fcm推送需要google服务）
        GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this);
    }

    @AfterViews
    void initView() {
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.setOnTabChangedListener(this);

        if (Build.VERSION.SDK_INT > 10) {
            mTabHost.getTabWidget().setShowDividers(0);
        }

        initTabs();
        mTabHost.setCurrentTab(startIndex);
//        UpdateUtils.update(this);
//        UmengUpdateAgent.setDeltaUpdate(false);
//        UmengUpdateAgent.update(this);
    }

    private void initTabs() {
        MainTab[] tabs = MainTab.values();
        final int size = tabs.length;
        for (int i = 0; i < size; i++) {
            MainTab mainTab = tabs[i];
            TabHost.TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));

            View indicator = inflateView(R.layout.v2_tab_indicator);
            ImageView icon = (ImageView) indicator.findViewById(R.id.tab_icon);
            icon.setImageResource(mainTab.getResIcon());
            TextView title = (TextView) indicator.findViewById(R.id.tab_titile);
            title.setText(getString(mainTab.getResName()));
            tab.setIndicator(indicator);
            tab.setContent(new TabHost.TabContentFactory() {

                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });

            mTabHost.addTab(tab, mainTab.getClz(), null);
        }
    }

    @Override
    public void onTabChanged(String tabId) {
        final int size = mTabHost.getTabWidget().getTabCount();
        for (int i = 0; i < size; i++) {
            View v = mTabHost.getTabWidget().getChildAt(i);
            if (i == mTabHost.getCurrentTab()) {
                mToolbarTitle.setText(MainTab.values()[i].getResName());

                v.findViewById(R.id.tab_icon).setSelected(true);
                v.findViewById(R.id.tab_titile).setSelected(true);
                v.findViewById(R.id.ly_tab).setSelected(true);
            } else {
                v.findViewById(R.id.tab_icon).setSelected(false);
                v.findViewById(R.id.tab_titile).setSelected(false);
                v.findViewById(R.id.ly_tab).setSelected(false);
            }
        }
        supportInvalidateOptionsMenu();
    }


    @Override
    public int getCurrentTab() {
        return mTabHost.getCurrentTab();
    }


    private long lastTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (System.currentTimeMillis() - lastTime > 1000) {
                    MToast.show(getString(R.string.click_again_exit));
                    lastTime = System.currentTimeMillis();
                    return true;
                }
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
